package com.kitchen.dao;

import com.kitchen.entity.Chef;
import com.kitchen.util.DatabaseUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 厨师DAO类
 */
public class ChefDAO {
    
    /**
     * 查询所有厨师
     */
    public List<Chef> findAll() {
        List<Chef> chefs = new ArrayList<>();
        String sql = "SELECT * FROM chef ORDER BY chef_id";
        
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        
        try {
            conn = DatabaseUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            
            while (rs.next()) {
                chefs.add(mapResultSetToChef(rs));
            }
        } catch (SQLException e) {
            throw new RuntimeException("查询厨师失败: " + e.getMessage(), e);
        } finally {
            DatabaseUtil.close(conn, pstmt, rs);
        }
        
        return chefs;
    }
    
    /**
     * 查询空闲的厨师
     */
    public List<Chef> findAvailableChefs() {
        List<Chef> chefs = new ArrayList<>();
        String sql = "SELECT * FROM chef WHERE status = '空闲' ORDER BY chef_id";
        
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        
        try {
            conn = DatabaseUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            
            while (rs.next()) {
                chefs.add(mapResultSetToChef(rs));
            }
        } catch (SQLException e) {
            throw new RuntimeException("查询厨师失败: " + e.getMessage(), e);
        } finally {
            DatabaseUtil.close(conn, pstmt, rs);
        }
        
        return chefs;
    }
    
    /**
     * 根据ID查询厨师
     */
    public Chef findById(Integer chefId) {
        String sql = "SELECT * FROM chef WHERE chef_id = ?";
        
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        
        try {
            conn = DatabaseUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, chefId);
            rs = pstmt.executeQuery();
            
            if (rs.next()) {
                return mapResultSetToChef(rs);
            }
        } catch (SQLException e) {
            throw new RuntimeException("查询厨师失败: " + e.getMessage(), e);
        } finally {
            DatabaseUtil.close(conn, pstmt, rs);
        }
        
        return null;
    }
    
    /**
     * 更新厨师状态
     */
    public boolean updateStatus(Integer chefId, String status) {
        String sql = "UPDATE chef SET status = ? WHERE chef_id = ?";
        
        Connection conn = null;
        PreparedStatement pstmt = null;
        
        try {
            conn = DatabaseUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, status);
            pstmt.setInt(2, chefId);
            
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            throw new RuntimeException("更新厨师状态失败: " + e.getMessage(), e);
        } finally {
            DatabaseUtil.close(conn, pstmt);
        }
    }
    
    private Chef mapResultSetToChef(ResultSet rs) throws SQLException {
        Chef chef = new Chef();
        chef.setChefId(rs.getInt("chef_id"));
        chef.setChefName(rs.getString("chef_name"));
        chef.setSpecialty(rs.getString("specialty"));
        chef.setStatus(rs.getString("status"));
        if (rs.getTimestamp("create_time") != null) {
            chef.setCreateTime(rs.getTimestamp("create_time").toLocalDateTime());
        }
        return chef;
    }
}

